package com.ruisi.weatherreport;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.ruisi.operateClasses.*;
import com.ruisi.thumbnail.ThumbnailManager;
import com.ruisi.thumbnail.ThumbnailManager.ImageLoadListener;
import com.ruisi.weatherreport.protocol.WeatherInfo;
import com.ruisi.weatherreport.protocol.WeatherInterfaces.DetailWeatherListener;
import com.ruisi.weatherreport.protocol.WeatherProtocol;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextPaint;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;



/**
 * 首页Activity
 * @author spring ming
 *
 */

public class MainActivity extends Activity implements DetailWeatherListener, ImageLoadListener{
	private String url = "http://h.hiphotos.baidu.com/album/w%3D2048/sign=e9bf8393ca1349547e1eef6462769358/d000baa1cd11728bf070b730c9fcc3cec3fd2c46.jpg";
		
	private TextView tv_todayweatherInfo;//当前天气描述
	private TextView tv_nowtemp;//当前温度
	private TextView tv_upDate;//更新日期
	private ImageView img_todayweather;//当前天气图片
	private GridView mgridview;//显示未来几天天气
	private Button btn_set;
	private Button btn_tend;
	private gridviewAdapter mAdapter;
	List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
	public static String futureTemp[]={"0","0","0","0","0","0"};//保存未来六天温度数据
	public static String futureDate[]={"0","0","0","0","0","0"};//保存未来六天日期
	Date now;
	SimpleDateFormat mFormat=new SimpleDateFormat("yy年MM月dd日");
	HashMap<String,String> map;
	
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tv_todayweatherInfo= (TextView)  findViewById(R.id.tv_taodayweatherInfo);
        tv_nowtemp=(TextView)  findViewById(R.id.tv_nowtemp);
        tv_upDate=(TextView)  findViewById(R.id.tv_upDate);
        img_todayweather = (ImageView) findViewById(R.id.img_todayweather);
        mgridview=(GridView)findViewById(R.id.grid);
        btn_set=(Button)findViewById(R.id.btn_set);
        btn_tend=(Button)findViewById(R.id.btn_tend);
        
        btn_set.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View V) {
				Intent intent=new Intent();
				intent.setClass(MainActivity.this, InfoActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.new_dync_in_from_right, R.anim.new_dync_out_to_left);
			}
		});
        
       btn_tend.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View V) {
				Intent intent=new Intent();
				intent.setClass(MainActivity.this, SqliteActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.new_dync_in_from_right, R.anim.new_dync_out_to_left);
			}
		});
      
        ThumbnailManager.defaultManager(getApplicationContext()).getRemoteImage(url, this);
        // 设置粗体
        TextPaint tp = tv_todayweatherInfo.getPaint();
        tp.setFakeBoldText(true);
        
        // 查询天气，加载数据
        this.queryWeatherInfo();
        
        //设置Gridview的Adapter
        mAdapter = new gridviewAdapter(this, list);
        mgridview.setAdapter(mAdapter);
        
    }
    
    /**
     * 发起网络请求，查询天气信息
     */
    private void queryWeatherInfo(){
    	String city = "深圳";
    	WeatherProtocol.queryDetailWeatherInfo(city, this);
    	
    }

	@Override
	public void onSuccess(WeatherInfo weatherInfo) {
		
		StringBuilder builder = new StringBuilder();
		
		builder.append("时间：" + weatherInfo.mDate + "  " + weatherInfo.mWeekday + "\n");
		builder.append("地点：" + weatherInfo.mCityName +  "\n");
		builder.append("温度：" + weatherInfo.mTempArray.get(0) + "\n");
		builder.append(weatherInfo.mWearIndex + "\n");
		builder.append(weatherInfo.mWearSuggestion + "\n");
		builder.append(weatherInfo.m48WearIndex + "\n");
		builder.append(weatherInfo.m48WearSuggestion + "\n");
		
		tv_todayweatherInfo.setText(builder.toString());
		tv_nowtemp.setText(weatherInfo.mTempArray.get(0));
		tv_upDate.setText(weatherInfo.mDate);
		
		for(int i=0;i<6;i++){
			futureTemp[i]=weatherInfo.mTempArray.get(i);
		}
		
		for(int i=0;i<6;i++){
			now=new Date(System.currentTimeMillis()+i*86400000);
			futureDate[i]=mFormat.format(now); 
		}
		
		for(int i=0;i<6;i++){
			map=new HashMap<String,String>();
			map.put("Date", futureDate[i]);
			map.put("temp", futureTemp[i]);
			list.add(map);	
		}
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void onFail(int errCode, String errDes) {
		tv_todayweatherInfo.setText(errDes);
		
	}

	@Override
	public void onImageLoadSuccess(String imgUrlOrPath, Bitmap bitmap) {
		img_todayweather.setImageBitmap(bitmap);
	}

	@Override
	public void onImageLoadFailed(String imgUrlOrPath, int errCode) {
		// TODO Auto-generated method stub
		
	}

}
